import os
import requests
from itertools import product

def get_name_parts_from_url():
    url = "https://gitee.com/guigu77/tool/raw/master/wechat/name.json"
    try:
        response = requests.get(url)
        response.raise_for_status()
        return response.json()
        print(f"字典读取成功！")
    except requests.RequestException as e:
        print(f"获取字典失败")
        return None

def generate_names(partial_name, name_parts):
    result_names = []
    x_positions = [i for i, char in enumerate(partial_name) if char == 'x' or char == '*']

    if not x_positions:
        result_names.append(partial_name)
    elif len(x_positions) == 1:
        for part in name_parts:
            new_name = partial_name[:x_positions[0]] + part + partial_name[x_positions[0] + 1:]
            result_names.append(new_name)
    else:
        all_combinations = product(name_parts, repeat=len(x_positions))
        for combination in all_combinations:
            new_name = partial_name
            for pos, part in zip(x_positions, combination):
                new_name = new_name[:pos] + part + new_name[pos + 1:]
            result_names.append(new_name)

    return result_names

def main():
    name_parts = get_name_parts_from_url()
    if name_parts is None:
        print("无法获取name_parts，程序退出")
        return

    partial_name = input("请输入模糊姓名 (例如 '某x某'): ")
    full_names = generate_names(partial_name, name_parts)
    
    file_path = "1.txt"
    with open(file_path, "w", encoding="utf-8") as file:
        for name in full_names:
            file.write(name + "\n")
    
    abs_path = os.path.abspath(file_path)
    print(f"生成的姓名已保存到文件: {abs_path}，共{len(full_names)}条")

if __name__ == "__main__":
    main()